package com.plian.system.controller.pf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.pf.BaseGrantSalaryDTO;
import com.plian.system.entity.pf.BaseGrantSalary;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pf.IBaseGrantSalaryService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pf.BaseGrantSalaryVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.wrapper.pf.BaseGrantSalaryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

/**
 * 职工工资发放主表_集团 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/basegrantsalary")
@Api(value = "职工工资发放主表", tags = "职工工资发放主表接口")
public class BaseGrantSalaryController extends BaseController {

	private IBaseGrantSalaryService baseGrantSalaryService;

	private FormWorkflowService formWorkflowService;

	private BaseGrantSalaryWrapper baseGrantSalaryWrapper;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入baseGrantSalary")
	public R<BaseGrantSalaryVO> detail(@RequestBody BaseGrantSalary baseGrantSalary) {
		BaseGrantSalary detail = baseGrantSalaryService.getOne(Condition.getQueryWrapper(baseGrantSalary));
		BaseGrantSalaryVO baseGrantSalaryVO = baseGrantSalaryWrapper.entityToVO(detail);
		if (baseGrantSalaryVO.getType() == 1){
			baseGrantSalaryVO.setFillingUnitName(baseGrantSalaryVO.getFillingUnitName() + "（单户）");
		}else {
			baseGrantSalaryVO.setFillingUnitName(baseGrantSalaryVO.getFillingUnitName() + "（合并）");
		}
		return baseGrantSalaryService.getDetailObj(baseGrantSalaryVO);
	}

	/**
	 * 获取初始化列表
	 */
	@PostMapping("/last")
	@ApiOperation(value = "获取初始化列表", notes = "传入baseGrantSalary")
	public R<BaseGrantSalaryVO> last(@RequestBody BaseGrantSalary baseGrantSalary) {
		return baseGrantSalaryService.getLastMonthObj(baseGrantSalary);
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	@ApiOperation(value = "分页", notes = "传入baseGrantSalary")
	public ListResult<BaseGrantSalaryVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("filling_month");
		}
		QueryWrapper<BaseGrantSalary> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new BaseGrantSalary()), entityList
		);
		TokenSearch<BaseGrantSalary> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		IPage<BaseGrantSalary> pages = baseGrantSalaryService.page(Condition.getPage(query), queryWrapper);
		List<BaseGrantSalaryVO> baseGrantSalaryVOS = baseGrantSalaryWrapper.entityToVO(pages.getRecords());
		ListResult<BaseGrantSalaryVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(baseGrantSalaryVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@ApiOperation(value = "新增或修改", notes = "传入baseGrantSalary")
	@LogOperation("职工工资发放-提交")
	public R submit(@Valid @RequestBody BaseGrantSalary baseGrantSalary) {
		String checkMessage = baseGrantSalaryService.checkMonth(baseGrantSalary);
		if (StringUtil.isNotBlank(checkMessage)){
			return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
		}

		boolean result = baseGrantSalaryService.submit(baseGrantSalary);
		HashMap<String, Object> processVariable = new HashMap<>();
		formWorkflowService.startProcessAndSimulate(String.valueOf(baseGrantSalary.getId()),baseGrantSalary.getFormType(),processVariable);

		return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入pd")
	@LogOperation("职工工资发放-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return baseGrantSalaryService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	/**
	 * 职工工资发放表-统计用(   年度)
	 */
	@PostMapping("/yearStatistics")
	@ApiOperation(value = "职工工资发放表-统计用(   年度)", notes = "传入baseGrantSalaryDTO")
	public R<ReportReturnVO> yearStatistics(@Valid @RequestBody(required = false) BaseGrantSalaryDTO baseGrantSalaryDTO) {
		ReportReturnVO reportReturnVO = baseGrantSalaryService.yearStatistics(baseGrantSalaryDTO);
		R<ReportReturnVO> objResult = new R<>(CommonCode.SUCCESS);
		objResult.setPageData(reportReturnVO);
		return objResult;
	}

	/**
	 * 树列表 分页
	 */
	@PostMapping("/treeList")
	@ApiOperation(value = "树列表 分页", notes = "传入query")
	public ListResult<PageData> treeList(@RequestBody Query query) {
		return baseGrantSalaryService.treeList(query);
	}

	/**
	 * 根据填报单位id、填报年月 获取合计数据
	 * @param baseGrantSalary
	 * @return
	 */
	@PostMapping("/getSummaryObj")
	@ApiOperation(value = "根据填报单位id、填报年月 获取合计数据", notes = "传入baseGrantSalary")
	public ObjResult getSummaryObj(@RequestBody BaseGrantSalary baseGrantSalary){
		return baseGrantSalaryService.getSummaryObj(baseGrantSalary);
	}

	/**
	 * 树
	 * @param baseGrantSalary
	 * @return
	 */
	@PostMapping("/tree")
	@ApiOperation(value = "树", notes = "传入baseGrantSalary")
	public ObjResult tree(@RequestBody BaseGrantSalary baseGrantSalary){
		return baseGrantSalaryService.tree(baseGrantSalary);
	}

	/**
	 * 工资填报树 新增
	 * @param baseGrantSalary
	 * @return
	 */
	@PostMapping("/add/tree")
	@Resubmit(delaySeconds = 0)
	@ApiOperation(value = "工资填报树 新增", notes = "传入baseGrantSalary")
	@LogOperation("职工工资发放-填报树新增")
	public R addTree(@RequestBody BaseGrantSalary baseGrantSalary){
		return baseGrantSalaryService.addTree(baseGrantSalary);
	}

	/**
	 * 获取下级已填报数据
	 * @param baseGrantSalary
	 * @return
	 */
	@PostMapping("/childList")
	@ApiOperation(value = "获取下级已填报数据", notes = "传入baseGrantSalary")
	public ObjResult childList(@RequestBody BaseGrantSalary baseGrantSalary){
		return baseGrantSalaryService.childList(baseGrantSalary);
	}

	/**
	 * 根据主表id 获取合计数据
	 * @param baseGrantSalary
	 * @return
	 */
	@PostMapping("/childSummary")
	@ApiOperation(value = "根据主表id 获取合计数据", notes = "传入baseGrantSalary")
	public R<BaseGrantSalary> childSummary(@RequestBody BaseGrantSalary baseGrantSalary){
		return baseGrantSalaryService.childSummary(baseGrantSalary);
	}

	/**
	 * 合计数据来源
	 * @param baseGrantSalaryDTO
	 * @return
	 */
	@PostMapping("/summaryDetail")
	@ApiOperation(value = "合计数据来源", notes = "传入baseGrantSalaryDTO")
	public ObjResult summaryDetail(@RequestBody BaseGrantSalaryDTO baseGrantSalaryDTO){
		return baseGrantSalaryService.summaryDetail(baseGrantSalaryDTO);
	}

	/**
	 * 集团工资 根据填报时间 新增
	 * @param baseGrantSalary
	 * @return
	 */
	@PostMapping("/initAdd")
	@Resubmit(delaySeconds = 0)
	@ApiOperation(value = "集团工资 根据填报时间 新增", notes = "传入baseGrantSalary")
	@LogOperation("职工工资发放-根据填报时间新增")
	public R initAdd(@RequestBody BaseGrantSalary baseGrantSalary){
		return baseGrantSalaryService.initAdd(baseGrantSalary);
	}

	/**
	 * 员工excel导出
	 * @param
	 * @return
	 */
	@PostMapping(value = "/exportExcel", produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "员工excel导出", notes = "传入pd")
	public void exportExcel(@RequestBody PageData pd, HttpServletResponse response, OutputStream os){
		baseGrantSalaryService.exportExcel(pd,response,os);
	}

//	@PostMapping(value = "/getSalaryWarning") TODO DEL
//	@ApiOperation(value = "工资预警表", notes = "传入年份")
//	public R<List<SalaryWarningVO>> getSalaryWarning(@RequestBody BaseGrantSalaryDTO baseGrantSalaryDTO){
//		return R.data(baseGrantSalaryService.getSalaryWarning(baseGrantSalaryDTO));
//	}

}
